"""
中间件【类
"""

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse, render, redirect


class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):
        # 0，要先排除掉哪些不需要登录就可以访问的页面
        # request.path_info     # 获取当前用户请求的url /login/
        if request.path_info in ['/login/', "/image/code/"]:
            return None

        # 1 读取当前访问的用户的session信息 如果读的到，说明已经登录
        info_dict = request.session.get('info')
        # print(info_dict)
        if info_dict:
            return None

        # 如果没有登录信息 回到登录页面
        return redirect('/login/')

    # def process_response(self, request, response):
    #     print('M1.出去了')
    #     return response

# 列子
# class M1(MiddlewareMixin):
#     """中间件1"""
#
#     def process_request(self, request):
#         # 如果没有返回值（返回None），继续向后走
#         # 如果有返回值 就返回那个 不在往后走
#         print('M1.进来了')
#
#     def process_response(self, request, response):
#         print('M1.出去了')
#         return response
